const { exec } = require('../db/mysql')

// 获取导航列表
const getNavList = params => {
  const { nid } = params
  let sql = `select * from nav`
  return exec(sql)
}

// 获取单个导航详情
const getNavDetail = params => {
  const { nid } = params
  let sql = `select * from nav `
  if (nid) {
    sql += `where nid='${nid}'`
  }
  return exec(sql)
}

// 添加导航
const addNav = params => {
  const { title = '', url = '', imgUrl = '', description = '', grou = '', pid = '' } = params
  let sql = `insert into nav (title, url, imgUrl, description, grou, pid) values ('${title}', '${url}', '${imgUrl}', '${description}', '${grou}', '${pid}')`
  return exec(sql)
}

// 更新导航
const updateNav = params => {
  const { nid, title, url, imgUrl, description, grou, pid } = params
  let sql = `update nav set title='${title}', url='${url}', imgUrl='${imgUrl}', description='${description}', grou='${grou}', pid='${pid}' where nid='${nid}'`
  return exec(sql).then(data => {
    if (data.affectedRows > 0) {
      return true
    } else {
      return false
    }
  })
}

// 删除导航
const deleteNav = params => {
  const { nid } = params
  let sql = `delete from nav where nid='${nid}'`
  return exec(sql).then(data => {
    if (data.affectedRows > 0) {
      return true
    } else {
      return false
    }
  })
}

module.exports = {
  getNavList,
  getNavDetail,
  addNav,
  updateNav,
  deleteNav
}
